package com.tebiecloud.order.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.order.client.model.entity.OrderGoods;
import com.tebiecloud.order.server.service.OrderGoodsService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 订单商品信息 前端控制器
 * @author tebie
 * @date 2019-10-07
 */
@RestController
@RequestMapping("/ordergoods")
public class OrderGoodsController {

    @Autowired
    private OrderGoodsService orderGoodsService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<OrderGoods>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        OrderGoods query = pageParams.mapToObject(OrderGoods.class);
        QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(orderGoodsService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<OrderGoods> get(@RequestParam("id") Long id) {
        OrderGoods entity = orderGoodsService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param orderId 订单id ,
     * @param userId 用户id,
     * @param storeId 门店id,
     * @param goodsId 商品id,
     * @param goodsSpecificationId 商品规格id,
     * @param goodsName 商品名称,
     * @param specificationName 规格名,
     * @param goodsPic 商品图片,
     * @param goodsPrice 商品单价（单位：分）,
     * @param amount 数量,
     * @param postagePrice 运费（单位：分）,
     * @param totalPrice 总费用（单位：分）,
     * @param sold 出售，0没有出售1已出售,
     * @param isPerennial 是否长期有效：0否，1是,
     * @param codeStime 有效开始时间,
     * @param codeEtime 有效结束时间,
     * @param remainWriteOffTimes 剩余可核销次数
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "orderId", required = false) Long orderId,
        @RequestParam(value = "userId", required = false) Long userId,
        @RequestParam(value = "storeId", required = false) Long storeId,
        @RequestParam(value = "goodsId", required = false) Long goodsId,
        @RequestParam(value = "goodsSpecificationId", required = false) Long goodsSpecificationId,
        @RequestParam(value = "goodsName", required = false) String goodsName,
        @RequestParam(value = "specificationName", required = false) String specificationName,
        @RequestParam(value = "goodsPic", required = false) String goodsPic,
        @RequestParam(value = "goodsPrice", required = false) Integer goodsPrice,
        @RequestParam(value = "amount", required = false) Integer amount,
        @RequestParam(value = "postagePrice", required = false) Integer postagePrice,
        @RequestParam(value = "totalPrice", required = false) Integer totalPrice,
        @RequestParam(value = "sold", required = false) Integer sold,
        @RequestParam(value = "isPerennial", required = false) Integer isPerennial,
        @RequestParam(value = "codeStime", required = false) Date codeStime,
        @RequestParam(value = "codeEtime", required = false) Date codeEtime,
        @RequestParam(value = "remainWriteOffTimes", required = false) Integer remainWriteOffTimes
    ) {
        OrderGoods entity = new OrderGoods();
        entity.setOrderId(orderId);
        entity.setUserId(userId);
        entity.setStoreId(storeId);
        entity.setGoodsId(goodsId);
        entity.setGoodsSpecificationId(goodsSpecificationId);
        entity.setGoodsName(goodsName);
        entity.setSpecificationName(specificationName);
        entity.setGoodsPic(goodsPic);
        entity.setGoodsPrice(goodsPrice);
        entity.setAmount(amount);
        entity.setPostagePrice(postagePrice);
        entity.setTotalPrice(totalPrice);
        entity.setSold(sold);
        entity.setIsPerennial(isPerennial);
        entity.setCodeStime(codeStime);
        entity.setCodeEtime(codeEtime);
        entity.setRemainWriteOffTimes(remainWriteOffTimes);
        orderGoodsService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param orderId 订单id 
     * @param userId 用户id
     * @param storeId 门店id
     * @param goodsId 商品id
     * @param goodsSpecificationId 商品规格id
     * @param goodsName 商品名称
     * @param specificationName 规格名
     * @param goodsPic 商品图片
     * @param goodsPrice 商品单价（单位：分）
     * @param amount 数量
     * @param postagePrice 运费（单位：分）
     * @param totalPrice 总费用（单位：分）
     * @param sold 出售，0没有出售1已出售
     * @param isPerennial 是否长期有效：0否，1是
     * @param codeStime 有效开始时间
     * @param codeEtime 有效结束时间
     * @param remainWriteOffTimes 剩余可核销次数
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "orderGoodsId", required = false) Long orderGoodsId,
            @RequestParam(value = "orderId", required = false) Long orderId,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "storeId", required = false) Long storeId,
            @RequestParam(value = "goodsId", required = false) Long goodsId,
            @RequestParam(value = "goodsSpecificationId", required = false) Long goodsSpecificationId,
            @RequestParam(value = "goodsName", required = false) String goodsName,
            @RequestParam(value = "specificationName", required = false) String specificationName,
            @RequestParam(value = "goodsPic", required = false) String goodsPic,
            @RequestParam(value = "goodsPrice", required = false) Integer goodsPrice,
            @RequestParam(value = "amount", required = false) Integer amount,
            @RequestParam(value = "postagePrice", required = false) Integer postagePrice,
            @RequestParam(value = "totalPrice", required = false) Integer totalPrice,
            @RequestParam(value = "sold", required = false) Integer sold,
            @RequestParam(value = "isPerennial", required = false) Integer isPerennial,
            @RequestParam(value = "codeStime", required = false) Date codeStime,
            @RequestParam(value = "codeEtime", required = false) Date codeEtime,
            @RequestParam(value = "remainWriteOffTimes", required = false) Integer remainWriteOffTimes
    ) {
        OrderGoods entity = new OrderGoods();
        entity.setOrderGoodsId(orderGoodsId);
        entity.setOrderId(orderId);
        entity.setUserId(userId);
        entity.setStoreId(storeId);
        entity.setGoodsId(goodsId);
        entity.setGoodsSpecificationId(goodsSpecificationId);
        entity.setGoodsName(goodsName);
        entity.setSpecificationName(specificationName);
        entity.setGoodsPic(goodsPic);
        entity.setGoodsPrice(goodsPrice);
        entity.setAmount(amount);
        entity.setPostagePrice(postagePrice);
        entity.setTotalPrice(totalPrice);
        entity.setSold(sold);
        entity.setIsPerennial(isPerennial);
        entity.setCodeStime(codeStime);
        entity.setCodeEtime(codeEtime);
        entity.setRemainWriteOffTimes(remainWriteOffTimes);
        orderGoodsService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            orderGoodsService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            orderGoodsService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
